import { h } from "koishi"

export const name = "🍠"

function react(session, emoji) {
  return session.onebot?._get("set_msg_emoji_like", { message_id: session.event.message.id, emoji_id: typeof emoji === "string" ? emoji.codePointAt(0) : emoji })
}

export function apply(ctx) {
  ctx = ctx.platform("onebot").guild()

  ctx.on("message", async session => {
    const texts = session.elements.filter(e => e.type === "text").map(e => e.attrs.content)
    if (
      群列表1.includes(session.channelId)
    ) {
      if (
        texts.some(
          t =>
            t.match(/傻\s*霸/) ||
            t.match(/水\s*哲/) ||
            群列表3.includes(session.channelId) && t.match(/咕\s*咕/)
        )
      )
        react(session, 356)
      else if (texts.some(t => t.match(/霸(?!\s*凌)/)))
        react(session, 283)
      if (texts.some(t => t.includes("苕")))
        react(session, "🍠")
      if (session.elements.some(e => e.type === "face" && e.attrs.id == 356))
        react(session, "❌")
    }
    if (群列表3.includes(session.channelId)) {
      if (texts.some(t => t.match(/(?<!\d\s*)66+(?!\s*\d)/) || t.includes("不知道") || t.includes("智齿")))
        react(session, "❌")
    }
    if (
      群列表2.includes(session.channelId)
    ) {
      if (texts.some(t => t.match(/琦|👾/)))
        react(session, "🍄")
      if (texts.some(t => t.match(/琪|🍄/)))
        react(session, "👾")
    }
    if (!喵opt_out用户列表.includes(session.userId) || ((await session.observeUser(["flag"])).flag & (1 << 28)))
      if (texts.some(t => t.includes("喵")))
        react(session, 307)
    for (const n of [111, 297, 307, 311, 318, 353, 355, 424])
      if (session.elements.some(e => e.type === "face" && e.attrs.id == n))
        react(session, n)
  })
}
